package com.my.commonlibrary.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.my.commonlibrary.R;
import com.my.pulltorefresh.library.Mode;
import com.my.pulltorefresh.library.PullToRefreshBase;
import com.my.pulltorefresh.library.PullToRefreshListView;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Author : Cooper
 * Time : 2016/9/26  14:44
 * Description : 下拉刷新上拉加载 BasePullToRefreshFragment基类
 */

public abstract class BasePullToRefreshFragment extends Fragment implements PullToRefreshBase.OnRefreshListener2{

    @Bind(R.id.mPullToRefreshListView)
    PullToRefreshListView mPullToRefreshListView;
    protected ListView  mListView;

    // 刷新类型 默认是上拉加载+下拉刷新
    private Mode mReFreshMode = Mode.BOTH;

    private HolderAdapter mHolderAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.base_frag_pull_to_refresh_layout, container, false);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        initPullToRefresh();
    }

    private void initPullToRefresh() {
        mPullToRefreshListView.setMode(mReFreshMode);
        mPullToRefreshListView.setOnRefreshListener(this);
        mListView = mPullToRefreshListView.getRefreshableView();
        mHolderAdapter = getAdapter();
        mListView.setAdapter(mHolderAdapter);
    }

    /**
     * 获取具体的Adapter
     * @return
     */
    protected abstract HolderAdapter getAdapter();

    /**
     * 刷新完成
     */
    protected void refreshComplete() {
        mPullToRefreshListView.onRefreshComplete();
    }

    /**
     * 设置刷新类型
     * @param mode
     */
    protected void setRefreshMode(Mode mode) {
        this.mReFreshMode = mode;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

}
